//
//  ViewController.swift
//  ZYUICommon
//
//  Created by Zhouyi on 01/04/2025.
//  Copyright (c) 2025 Zhouyi. All rights reserved.
//

import UIKit
import ZYUICommon

class ViewController: UIViewController {
    
    /// 便捷使用链式创建UI控件
    lazy var titleLabel: UILabel = {
        return UILabel()
            .zyFrame(CGRect(x: 0, y: zStatusBarHeight, width: zScreenWidth, height: zNavBarHeight))
            .zyFont(18, .semibold)
            .zyTextColor(ZYUtil.colorWithHex("#333333"))
            .zyTextAlignment(.center)
            .zyText("ZYUICommon")
    }()
    
    lazy var contentLabel: UILabel = {
        return UILabel()
            .zyFrame(CGRect(x: 0, y: zTopHeight + 100, width: zScreenWidth, height: zNavBarHeight))
            .zyFont(24, .medium)
            .zyTextColor(ZYUtil.colorRandom())
            .zyTextAlignment(.center)
            .zyText("0")
    }()
    
    lazy var counterButton: UIButton = {
        return UIButton(type: .custom)
            .zyFrame(CGRect(x: zScreenWidth - 64, y: zScreenHeight - zSafeBottomMargin - 64, width: 44, height: 44))
            .zyFont(16)
            .zyNormalTextColor(.white)
            .zyBackgroundColor(ZYUtil.colorRandom())
            .zyNormalText("+1")
            .zyAllCornerRadius(22)
            .zyAddTarget { [weak self] in
                guard let weakSelf = self else { return }
                weakSelf.count += 1
                weakSelf.contentLabel.text = "\(weakSelf.count)"
            }
    }()
    
    lazy var alertButton: UIButton = {
        return UIButton(type: .custom)
            .zyFrame(CGRect(x: zScreenWidth - 64, y: zScreenHeight - zSafeBottomMargin - 128, width: 44, height: 44))
            .zyFont(16)
            .zyNormalTextColor(.white)
            .zyBackgroundColor(ZYUtil.colorRandom())
            .zyNormalText("弹出")
            .zyAllCornerRadius(22)
            .zyAddTarget { [weak self] in
                guard let weakSelf = self else { return }
                weakSelf.showAlert()
            }
    }()
    
    lazy var listButton: UIButton = {
        return UIButton(type: .custom)
            .zyFrame(CGRect(x: zScreenWidth - 64, y: zScreenHeight - zSafeBottomMargin - 192, width: 44, height: 44))
            .zyFont(16)
            .zyNormalTextColor(.white)
            .zyBackgroundColor(ZYUtil.colorRandom())
            .zyNormalText("展示")
            .zyAllCornerRadius(22)
            .zyAddTarget { [weak self] in
                guard let weakSelf = self else { return }
                weakSelf.showListAlert()
            }
    }()
    
    lazy var cacheButton: UIButton = {
        return UIButton(type: .custom)
            .zyFrame(CGRect(x: zScreenWidth - 64, y: zScreenHeight - zSafeBottomMargin - 256, width: 44, height: 44))
            .zyFont(16)
            .zyNormalTextColor(.white)
            .zyBackgroundColor(ZYUtil.colorRandom())
            .zyNormalText("存储")
            .zyAllCornerRadius(22)
            .zyAddTarget { [weak self] in
                self?.cache()
            }
    }()
    
    lazy var readButton: UIButton = {
        return UIButton(type: .custom)
            .zyFrame(CGRect(x: zScreenWidth - 64, y: zScreenHeight - zSafeBottomMargin - 320, width: 44, height: 44))
            .zyFont(16)
            .zyNormalTextColor(.white)
            .zyBackgroundColor(ZYUtil.colorRandom())
            .zyNormalText("取")
            .zyAllCornerRadius(22)
            .zyAddTarget { [weak self] in
                self?.read()
            }
    }()
    
    lazy var reloadTagButton: UIButton = {
        return UIButton(type: .custom)
            .zyFrame(CGRect(x: zScreenWidth - 188, y: zScreenHeight - zSafeBottomMargin - 64, width: 104, height: 44))
            .zyFont(16)
            .zyNormalTextColor(.white)
            .zyBackgroundColor(ZYUtil.colorRandom())
            .zyNormalText("tagView")
            .zyAllCornerRadius(22)
            .zyAddTarget { [weak self] in
                let vc = ZYTagViewController()
                vc.modalPresentationStyle = .fullScreen
                self?.present(vc, animated: true)
            }
    }()
    
    lazy var timerButton: UIButton = {
        return UIButton(type: .custom)
            .zyFrame(CGRect(x: zScreenWidth - 188, y: zScreenHeight - zSafeBottomMargin - 128, width: 104, height: 44))
            .zyFont(16)
            .zyNormalTextColor(.white)
            .zyBackgroundColor(ZYUtil.colorRandom())
            .zyNormalText("定时器(开始)")
            .zySelectedText("定时器(结束)")
            .zyAllCornerRadius(22)
            .zyAddTarget { [weak self] in
                self?.timerAction()
            }
    }()
    
    lazy var draggableButton: UIButton = {
        return UIButton(type: .custom)
            .zyFrame(CGRect(x: zScreenWidth - 188, y: zScreenHeight - zSafeBottomMargin - 192, width: 104, height: 44))
            .zyFont(16)
            .zyNormalTextColor(.white)
            .zyBackgroundColor(ZYUtil.colorRandom())
            .zyNormalText("draggable")
            .zyAllCornerRadius(22)
            .zyAddTarget { [weak self] in
                let vc = ZYDraggableController()
                vc.modalPresentationStyle = .fullScreen
                self?.present(vc, animated: true)
            }
    }()
    
    lazy var toastButton: UIButton = {
        return UIButton(type: .custom)
            .zyFrame(CGRect(x: zScreenWidth - 188, y: zScreenHeight - zSafeBottomMargin - 256, width: 104, height: 44))
            .zyFont(16)
            .zyNormalTextColor(.white)
            .zyBackgroundColor(ZYUtil.colorRandom())
            .zyNormalText("toast")
            .zyAllCornerRadius(22)
            .zyAddTarget { [weak self] in
                self?.showToast()
            }
    }()
    
    lazy var progressButton: UIButton = {
        return UIButton(type: .custom)
            .zyFrame(CGRect(x: zScreenWidth - 188, y: zScreenHeight - zSafeBottomMargin - 320, width: 104, height: 44))
            .zyFont(16)
            .zyNormalTextColor(.white)
            .zyBackgroundColor(ZYUtil.colorRandom())
            .zyNormalText("progress")
            .zyAllCornerRadius(22)
            .zyAddTarget { [weak self] in
                self?.showProgress()
            }
    }()
    
    lazy var timer: ZYTimer = {
        return ZYTimer.init(interval: 1) { [weak self] in
            self?.count += 1
            debugPrint("\(self?.count ?? 0)")
        }
    }()
    
    lazy var progressTimer: ZYTimer = {
        return ZYTimer.init(interval: 1) { [weak self] in
            self?.progress += 0.1
            ZYProgress.shared.progress = (self?.progress ?? 0) / 1
        }
    }()
    
    var count = 0
    
    var progress: CGFloat = 0
    
    let cacheKey = "lzy"
    
    var progressShowing = true

    override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = .white
        view.zyAddChild([
            titleLabel,
            contentLabel,
            counterButton,
            alertButton,
            listButton,
            cacheButton,
            readButton,
            reloadTagButton,
            timerButton,
            draggableButton,
            toastButton,
            progressButton
        ])
    }
    
    func showAlert() {
        let alert = ZYCommonConfirmationAlert()
        alert.titleText = "对于非常短或非常长的文本，以及包含特殊字符或表情符号的情况，务必进行充分的测试，以确保计算结果符合预期。"
        alert.subtitleText = "通过以上改进，你应该能够更准确地计算出给定宽度下文本的高度。这种方法不仅简单易用，而且由于它依赖于 UIKit 内置的布局引擎，通常能提供良好的精度。"
        alert.layoutAlert()
        alert.show()
    }
    
    func showListAlert() {
        let alert = ZYCommonListAlert()
        alert.displayTitle = false
        alert.dataSource = ["登录", "注册", "找回密码"]
        alert.cancelStyle = ("取消",
                             UIFont.systemFont(ofSize: 20, weight: .semibold),
                             UIColor.red,
                             44
        )
        alert.layoutAlert()
        alert.show()
    }
    
    func cache() {
        let user = User()
        user.name = "ZY"
        user.age = 22
        
        ZYCache.shared.cacheObject(cacheKey, user)
    }
    
    func read() {
        let user = ZYCache.shared.readObject(forKey: cacheKey, as: User.self)
        debugPrint(user?.name ?? "")
        debugPrint(user?.age ?? 0)
    }
    
    func timerAction() {
        timerButton.isSelected = !timerButton.isSelected
        if timerButton.isSelected {
            timer.start()
        } else {
            timer.invalidate()
        }
    }
    
    func showToast() {
        ZYToast.shared.show("我是吐司")
    }
    
    func showProgress() {
        progressShowing = !progressShowing
        if progressShowing {
            ZYProgress.shared.dismiss()
            progressTimer.invalidate()
            progress = 0
        } else {
            ZYProgress.shared.mode = .line
            ZYProgress.shared.show("正在下载...")
            progressTimer.start()
        }
    }

}

class User: Codable {
    var name: String?
    var age: Int?
}

